/**
 * © 2012 HaoTu.la
 */
(function($) {	
	$.slide = function(el, options) {
		var slider = el;

		slider.id = el.attr('id');
		slider.options = $.extend({}, $.slide.defaults, options);
		slider.container = null;
		slider.ul = null;
		slider.mod = 0;
		slider.width = {img: 0, li: 0, ul: 0};
		slider.length = 0;
		slider.num = 0;
		slider.timer = null;
		slider.init = function() {
			var self = this;
			var p = typeof(this.options.imageMargin) == 'undefined' ? 0 : this.options.imageMargin;
			this.num = this.options.imageNumber;
			this.container = $('#' + this.id);
			this.ul = $('#' + this.id + ' ul');
			this.length = $('#' + this.id + ' li').length;
			this.width.img = this.container.width() / this.num - p;
			this.width.li = this.width.img + p;
			this.width.ul = this.width.li * this.length;
			this.mod = this.length % this.num;
			var repeat = 1;
			if(this.num <= this.length) {
				var m = (this.num * 3) % this.length == 0 ? 0 : 1;
				repeat = parseInt(this.num * 3 / this.length) + m;
			}
			$('#' + this.id + ' img').css({'width': this.width.img + 'px'});
			var lis = $('#' + this.id + ' li');
			for(var k = 1; k <= repeat; k++) {
				lis.each(function(i){
					self.ul.append($(this).clone(true));
				});
			}
				
			this.ul.css({'width': (this.width.ul * 2 * repeat) + 'px'});
			
			this.container.parent('div').find('a.leftButton').click(function(){
				self._left();
				return false;
			});
			this.container.parent('div').find('a.rightButton').click(function(){
				self._right();
				return false;
			});
			this.container.parent('div')
				.mouseover(function(){clearTimeout(self.timer);})
				.mouseout(function(){self._timer();})
				.mouseout();
		};
		slider._timer = function() {
			var self = this;
			this.timer = setTimeout(function(){
				self._left();
				self._timer();
			}, this.options.slideTimer);
		};
		slider._left = function() {
			if(this.num >= this.length || this.ul.attr('_animate') == '1') {
				return false;
			}
			var l = parseInt(this.ul.css('margin-left').replace('px', ''));
			var a = this.mod == 0 ? 0 : 1;
			var w = (parseInt(this.length / this.num) + a) * (this.width.li * this.num);
			if(l <= - w) {
				var w1 = (Math.abs(parseInt(l / this.width.li)) - this.length) * this.width.li;
				this.ul.css({'margin-left': - w1 + 'px'});
				this._left();
				return false;
			}
			this.ul.stop().attr('_animate', '1')
					.animate({'margin-left': (l - this.container.width()) + 'px'}, 'slow', function(){
						$(this).removeAttr('_animate');
					});
		};
		slider._right = function() {
			if(this.num >= this.length || this.ul.attr('_animate') == '1') {
				return false;
			}
			var l = parseInt(this.ul.css('margin-left').replace('px', ''));
			if(l >= - this.num * this.width.li) {
				var w1 = l == 0 ? this.width.ul : this.length * this.width.li + Math.abs(l);
				this.ul.css({'margin-left': - w1 + 'px'});
				this._right();
				return false;
			}
			this.ul.stop().attr('_animate', '1')
					.animate({'margin-left': (l + this.container.width()) + 'px'}, 'slow', function(){
						$(this).removeAttr('_animate');
					});
		};
		slider.init();
	};
	$.fn.slide = function(options) {
		return $(this).each(function(){
			new $.slide($(this), options);
		});
	};
	$.slide.defaults = {
		imageNumber: 8,
		imageMargin: 0,
		slideTimer: 5000
	};
	
})(jQuery);
